package com.kuizii.discovery.ribbon;

import com.kuizii.discovery.client.DefaultDiscoveryServer;
import com.kuizii.discovery.client.DiscoveryProperties;
import com.kuizii.discovery.client.DiscoveryServer;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.ribbon.PropertiesFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author renxiaodong
 */
@Configuration
public class LiteRibbonClientConfiguration {
    @Autowired
    private PropertiesFactory propertiesFactory;

    @Bean
    @ConditionalOnMissingBean
    public ServerList<?> ribbonServerList(IClientConfig config, DiscoveryProperties discoveryProperties, DiscoveryServer discoveryServer) {
        if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
            ServerList serverList = this.propertiesFactory.get(ServerList.class, config, config.getClientName());
            return serverList;
        }

        return discoveryServer.getServerList();
    }

    @Bean
    @ConditionalOnMissingBean
    public LiteServerIntrospector liteServerIntrospector() {
        return new LiteServerIntrospector();
    }
}
